package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPighouseDAO;

import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.PigHouseType;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.TaskPighouse;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for PigHouse entities
 * 
 */

@Service("PigHouseService")

@Transactional
public class PigHouseServiceImpl implements PigHouseService {

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages PigHouseType entities
	 * 
	 */
	@Autowired
	private PigHouseTypeDAO pigHouseTypeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseDAO taskPighouseDAO;

	/**
	 * Instantiates a new PigHouseServiceImpl.
	 *
	 */
	public PigHouseServiceImpl() {
	}

	/**
	 * Save an existing PigHouseType entity
	 * 
	 */
	@Transactional
	public PigHouse savePigHousePigHouseType(Integer id, PigHouseType related_pighousetype) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(id, -1, -1);
		PigHouseType existingpigHouseType = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(related_pighousetype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigHouseType != null) {
			existingpigHouseType.setId(related_pighousetype.getId());
			existingpigHouseType.setName(related_pighousetype.getName());
			existingpigHouseType.setMemo(related_pighousetype.getMemo());
			related_pighousetype = existingpigHouseType;
		}

		pighouse.setPigHouseType(related_pighousetype);
		related_pighousetype.getPigHouses().add(pighouse);
		pighouse = pigHouseDAO.store(pighouse);
		pigHouseDAO.flush();

		related_pighousetype = pigHouseTypeDAO.store(related_pighousetype);
		pigHouseTypeDAO.flush();

		return pighouse;
	}

	/**
	 * Delete an existing PigHouse entity
	 * 
	 */
	@Transactional
	public void deletePigHouse(PigHouse pighouse) {
		pigHouseDAO.remove(pighouse);
		pigHouseDAO.flush();
	}

	/**
	 * Delete an existing PigHouseType entity
	 * 
	 */
	@Transactional
	public PigHouse deletePigHousePigHouseType(Integer pighouse_id, Integer related_pighousetype_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id, -1, -1);
		PigHouseType related_pighousetype = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(related_pighousetype_id, -1, -1);

		pighouse.setPigHouseType(null);
		related_pighousetype.getPigHouses().remove(pighouse);
		pighouse = pigHouseDAO.store(pighouse);
		pigHouseDAO.flush();

		related_pighousetype = pigHouseTypeDAO.store(related_pighousetype);
		pigHouseTypeDAO.flush();

		pigHouseTypeDAO.remove(related_pighousetype);
		pigHouseTypeDAO.flush();

		return pighouse;
	}

	/**
	 * Delete an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public PigHouse deletePigHouseTaskPighouses(Integer pighouse_id, Integer related_taskpighouses_id) {
		TaskPighouse related_taskpighouses = taskPighouseDAO.findTaskPighouseByPrimaryKey(related_taskpighouses_id, -1, -1);

		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id, -1, -1);

		related_taskpighouses.setPigHouse(null);
		pighouse.getTaskPighouses().remove(related_taskpighouses);

		taskPighouseDAO.remove(related_taskpighouses);
		taskPighouseDAO.flush();

		return pighouse;
	}

	/**
	 * Delete an existing Inspection entity
	 * 
	 */
	@Transactional
	public PigHouse deletePigHouseInspections(Integer pighouse_id, Integer related_inspections_id) {
		Inspection related_inspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections_id, -1, -1);

		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id, -1, -1);

		related_inspections.setPigHouse(null);
		pighouse.getInspections().remove(related_inspections);

		inspectionDAO.remove(related_inspections);
		inspectionDAO.flush();

		return pighouse;
	}

	/**
	 * Save an existing Inspection entity
	 * 
	 */
	@Transactional
	public PigHouse savePigHouseInspections(Integer id, Inspection related_inspections) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(id, -1, -1);
		Inspection existinginspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspections != null) {
			existinginspections.setId(related_inspections.getId());
			existinginspections.setTime(related_inspections.getTime());
			existinginspections.setMemo(related_inspections.getMemo());
			related_inspections = existinginspections;
		}

		related_inspections.setPigHouse(pighouse);
		pighouse.getInspections().add(related_inspections);
		related_inspections = inspectionDAO.store(related_inspections);
		inspectionDAO.flush();

		pighouse = pigHouseDAO.store(pighouse);
		pigHouseDAO.flush();

		return pighouse;
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public PigHouse savePigHousePigsties(Integer id, Pigsty related_pigsties) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(id, -1, -1);
		Pigsty existingpigsties = pigstyDAO.findPigstyByPrimaryKey(related_pigsties.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsties != null) {
			existingpigsties.setId(related_pigsties.getId());
			existingpigsties.setNumber(related_pigsties.getNumber());
			existingpigsties.setRfid(related_pigsties.getRfid());
			existingpigsties.setCapacity(related_pigsties.getCapacity());
			existingpigsties.setMemo(related_pigsties.getMemo());
			related_pigsties = existingpigsties;
		}

		related_pigsties.setPigHouse(pighouse);
		pighouse.getPigsties().add(related_pigsties);
		related_pigsties = pigstyDAO.store(related_pigsties);
		pigstyDAO.flush();

		pighouse = pigHouseDAO.store(pighouse);
		pigHouseDAO.flush();

		return pighouse;
	}

	/**
	 * Return all PigHouse entity
	 * 
	 */
	@Transactional
	public List<PigHouse> findAllPigHouses(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<PigHouse>(pigHouseDAO.findAllPigHouses(startResult, maxRows));
	}

	/**
	 * Return a count of all PigHouse entity
	 * 
	 */
	@Transactional
	public Integer countPigHouses() {
		return ((Long) pigHouseDAO.createQuerySingleResult("select count(o) from PigHouse o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public PigHouse deletePigHousePigsties(Integer pighouse_id, Integer related_pigsties_id) {
		Pigsty related_pigsties = pigstyDAO.findPigstyByPrimaryKey(related_pigsties_id, -1, -1);

		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id, -1, -1);

		related_pigsties.setPigHouse(null);
		pighouse.getPigsties().remove(related_pigsties);

		pigstyDAO.remove(related_pigsties);
		pigstyDAO.flush();

		return pighouse;
	}

	/**
	 * Save an existing PigHouse entity
	 * 
	 */
	@Transactional
	public void savePigHouse(PigHouse pighouse) {
		PigHouse existingPigHouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());

		if (existingPigHouse != null) {
			if (existingPigHouse != pighouse) {
				existingPigHouse.setId(pighouse.getId());
				existingPigHouse.setNumber(pighouse.getNumber());
				existingPigHouse.setRfid(pighouse.getRfid());
				existingPigHouse.setMemo(pighouse.getMemo());
			}
			pighouse = pigHouseDAO.store(existingPigHouse);
		} else {
			pighouse = pigHouseDAO.store(pighouse);
		}
		pigHouseDAO.flush();
	}

	/**
	 * Save an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public PigHouse savePigHouseTaskPighouses(Integer id, TaskPighouse related_taskpighouses) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(id, -1, -1);
		TaskPighouse existingtaskPighouses = taskPighouseDAO.findTaskPighouseByPrimaryKey(related_taskpighouses.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPighouses != null) {
			existingtaskPighouses.setId(related_taskpighouses.getId());
			existingtaskPighouses.setMemo(related_taskpighouses.getMemo());
			related_taskpighouses = existingtaskPighouses;
		}

		related_taskpighouses.setPigHouse(pighouse);
		pighouse.getTaskPighouses().add(related_taskpighouses);
		related_taskpighouses = taskPighouseDAO.store(related_taskpighouses);
		taskPighouseDAO.flush();

		pighouse = pigHouseDAO.store(pighouse);
		pigHouseDAO.flush();

		return pighouse;
	}

	/**
	 */
	@Transactional
	public PigHouse findPigHouseByPrimaryKey(Integer id) {
		return pigHouseDAO.findPigHouseByPrimaryKey(id);
	}

	/**
	 * Load an existing PigHouse entity
	 * 
	 */
	@Transactional
	public Set<PigHouse> loadPigHouses() {
		return pigHouseDAO.findAllPigHouses();
	}
}
